home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _armbob / !ArmBob_progs_h_string_string < prev    next >
Encoding:
Text File  |  1994-06-06  |  838 b   |  41 lines

  1. /* GCW     06/06/94   */
  2. /* convert a number n to a signed string to base b */
  3. string(n,b)  
  4. {
  5.  local neg,s,d;
  6.  if (n == 0) return ("0");
  7.  if (neg = (n<0)) n = -n;
  8.  s = "";
  9.  while (n)
  10.    {
  11.     d = n%b;
  12.     s = ((d<10)?(d +'0'):(d+'W'))+s;
  13.     n /= b;
  14.    }
  15.  return (((neg)?"-":"")+s);
  16. }
  17.  
  18. /* convert a number n to an unsigned hexadecimal string */
  19. hex(n)
  20. {
  21.  local s,byte,top,bottom,b,i,zero;
  22.  s = "&";zero = TRUE;i = 4;
  23.  b = @(newstring(4));
  24.  in b put { n; }
  25.  while (i>0 && zero)
  26.    {
  27.     i--;
  28.     if (byte=`(b+i)) zero = FALSE;
  29.    }
  30.  top = byte/16; bottom = byte%16;
  31.  if (top) s += (top<10)?(top+'0'):(top+'W');
  32.  s += (bottom<10)?(bottom+'0'):(bottom+'W'); 
  33.  while (i>0)
  34.    {
  35.     i--; byte = `(b+i); top = byte/16; bottom = byte%16;
  36.     s += (top<10)?(top+'0'):(top+'W');
  37.     s += (bottom<10)?(bottom+'0'):(bottom+'W');
  38.    }
  39.  return s;
  40.